/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 d3leaf@126.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.mapperdb.db.pro;

import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.session.Configuration;

import com.mapperdb.builder.StatementBuilder;
import com.mapperdb.db.Config;
import com.mapperdb.db.ResultEntity;
import com.mapperdb.provider.DbProvider;

public abstract class BasePro<T extends DbProvider> {
	protected Configuration configuration;
	protected Class<T> providerClass;
	protected SqlCommandType sqlCommandType;
	protected Class<?> returnType;
	protected Class<?> entityClass;
	protected String mappedStatementId;
	protected ResultEntity resultEntity;
	protected Class<?>[] types;
	protected Object parameterObject;

	public BasePro(Config config, Class<?> entityClass) {
		this.configuration = config.getConfiguration();
		this.entityClass = entityClass;
		this.resultEntity = config.getResultEntity(entityClass);
	}

	// -------------------------------------------------
	// my
	// -------------------------------------------------
	protected boolean hasId(String name) {
		this.mappedStatementId = generateMappedStatementId(providerClass.getName() + "." + name, returnType, types);
		return configuration.hasStatement(mappedStatementId);
	}

	protected String generateMappedStatementId(String name, Class<?> returnType, Class<?>... parameterTypes) {
		StringBuilder suffix = new StringBuilder();

		// return
		suffix.append("[").append(returnType.getSimpleName()).append("]");
		// param
		for (Class<?> c : parameterTypes) {
			suffix.append("-");
			suffix.append(c.getSimpleName());
		}
		if (suffix.length() < 1) {
			suffix.append("-void");
		}
		return name + suffix;
	}

	protected String buildMappedStatement(String name) {
		if (hasId(name)) return mappedStatementId;

		StatementBuilder dbBuilder = new StatementBuilder(configuration, providerClass, mappedStatementId);
		dbBuilder.setName(name);
		dbBuilder.setParameterTypes(types);
		dbBuilder.setSqlCommandType(sqlCommandType);
		dbBuilder.setReturnType(returnType);
		dbBuilder.setResultEntity(resultEntity);
		dbBuilder.parseStatement();

		dbBuilder.buildSqlSource(resultEntity, providerClass, name);
		return mappedStatementId;
	}
}
